import os
import sys

from common.content_type import ContentType
from common.user_agent import useragent
from core.result_base import ResultBase
from api.dashboard import dashboard
from common.logger import logger
import urllib3
urllib3.disable_warnings()

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(BASE_PATH)


#def dashboard_create(access_token,name,proj_id,label,comment,meta,category):
def dashboard_create(access_token,name,proj_id):
    """
    创建仪表盘
    :param name: 仪表盘名称
    :param proj_id: 父文件夹id
    :param label:
    :param comment:
    :param meta:
    :param category:
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "name":name,
        "proj_id":proj_id
        # "label":label,
        # "comment":comment,
        # "meta":meta,
        # "category":category
    }

    headers = {
        "Content-Type": ContentType.MULTIPART_FORM_DATA.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token":access_token
    }

    res = dashboard.create(params=payload, headers=headers,cookies=cookies)

    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("创建仪表盘 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def dashboard_modify(access_token,_t,name,proj_id,_xsrf):
    """
    编辑仪表盘
    :param _t: 13位的时间戳
    :param name: 文件夹名称
    :param proj_id : 要修改的文件夹id
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "_t": _t,
        "data":{"name": name},
        "proj_id": proj_id,
        "_xsrf":_xsrf
    }

    header = {
        "Content-Type": "application/json;charset=utf-8"
   }

    res = project.modify(access_token,json=payload, headers=header)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("修改文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result



def dashboard_list(access_token,_t,_xsrf,own_only,proj_id):
    """
    查询仪表盘
    :param _t: 13位的时间戳
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "_t": _t,
        "_xsrf":_xsrf,
        "own_only":own_only,
        "proj_id":proj_id
    }

    header = {
        "Content-Type": "application/json;charset=utf-8",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.66 Safari/537.36"
   }

    res = dashboard.list(access_token,params=payload, headers=header)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("查询仪表盘 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result



# def project_delete(access_token,_t,proj_id,_xsrf):
#     """
#     删除文件夹
#     :param _t: 13位的时间戳
#     :param _xsrf :
#     :return: 自定义的关键字返回结果 result
#     """
#     result = ResultBase()
#     payload = {
#         "_t": _t,
#         "proj_id": proj_id,
#         "_xsrf":_xsrf
#     }
#
#     header = {
#         "Content-Type": "application/json;charset=utf-8"
#    }
#
#     res = project.delete(access_token,params=payload, headers=header)
#     result.success = False
#     if res.json()["status"] == "0":
#         result.success = True
#     else:
#         result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
#     result.payload = res.json()
#     result.response = res
#     logger.info("删除文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
#     return result
#
#
# def project_move(access_token,_t,proj_id,sort,_xsrf):
#     """
#     移动文件夹
#     :param _t: 13位的时间戳
#     :param _xsrf :
#     :return: 自定义的关键字返回结果 result
#     """
#     result = ResultBase()
#     payload = {
#         "_t": _t,
#         "proj_id": proj_id,
#         "sort":sort,
#         "_xsrf":_xsrf
#     }
#
#     header = {
#         "Content-Type": "application/json;charset=utf-8",
#         "access_token":"273f26cc95427aa615ac07c0983f7a3f7454a34ff8cf834f"
#         #"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.66 Safari/537.36"
#         # "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
#
#    }
#
#     res = project.move(access_token,data=payload, headers=header)
#     result.success = False
#     if res.json()["status"] == "0":
#         result.success = True
#     else:
#         result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
#     result.payload = res.json()
#     result.response = res
#     logger.info("移动文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
#     return result











